package f.h.dragon.test.asm;

import java.util.Arrays;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * ParseClassTester: parse class 
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/03/31
 * @since 1.0
 */
public class ParseClassTester {

	/**
	 * test... 
	 * 
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception{
		ParseClass pc = new ParseClass();
		ClassReader cr = new ClassReader("java.lang.Byte");
		cr.accept(pc, 0);
	}
	
}

/**
 * ParseClass : parse class by ClassVisitor
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/03/31
 * @since 1.0
 */
class ParseClass extends ClassVisitor {

	//Constructor
	/**
	 * 构建指定api的解析类对象
	 * 
	 * @param see {@link Opcodes#ASM4}
	 */
	public ParseClass() {
		super(Opcodes.ASM4);
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
	 */
	@Override
	public void visit(int version, int access, String name, String signature,
			String superName, String[] interfaces) {
		System.out.println("visit:--------------------------------------------------");
		System.out.println("version = " + version);
		System.out.println("access = " + access);
		System.out.println("name = " + name);
		System.out.println("signature = " + signature);
		System.out.println("superName = " + superName);
		System.out.println("interfaces = " + Arrays.toString(interfaces));
		System.out.println("end visit==================================================");
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitSource(java.lang.String, java.lang.String)
	 */
	@Override
	public void visitSource(String source, String debug) {
		System.out.println("visitSource:--------------------------------------------------");
		System.out.println("source = " + source);
		System.out.println("debug = " + debug);
		System.out.println("end visitSource==================================================");
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitOuterClass(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void visitOuterClass(String owner, String name, String desc) {
		System.out.println("visitOuterClass:--------------------------------------------------");
		System.out.println("owner = " + owner);
		System.out.println("name = " + name);
		System.out.println("desc = " + desc);
		System.out.println("end visitOuterClass==================================================");
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitAnnotation(java.lang.String, boolean)
	 */
	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		System.out.println("visitAnnotation:--------------------------------------------------");
		System.out.println("desc = " + desc);
		System.out.println("visible = " + visible);
		System.out.println("end visitAnnotation==================================================");
		return null;
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitAttribute(org.objectweb.asm.Attribute)
	 */
	@Override
	public void visitAttribute(Attribute attr) {
		System.out.println("visitAttribute:--------------------------------------------------");
		System.out.println("attr = " + attr);
		System.out.println("end visitAttribute==================================================");
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitInnerClass(java.lang.String, java.lang.String, java.lang.String, int)
	 */
	@Override
	public void visitInnerClass(String name, String outerName,
			String innerName, int access) {
		System.out.println("visitInnerClass:--------------------------------------------------");
		System.out.println("name = " + name);
		System.out.println("outerName = " + outerName);
		System.out.println("innerName = " + innerName);
		System.out.println("access = " + access);
		System.out.println("end visitInnerClass==================================================");
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
	 */
	@Override
	public FieldVisitor visitField(int access, String name, String desc,
			String signature, Object value) {
		System.out.println("visitField:--------------------------------------------------");
		System.out.println("access = " + access);
		System.out.println("name = " + name);
		System.out.println("desc = " + desc);
		System.out.println("signature = " + signature);
		System.out.println("value = " + value);
		System.out.println("end visitField==================================================");
		return null;
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
	 */
	@Override
	public MethodVisitor visitMethod(int access, String name, String desc,
			String signature, String[] exceptions) {
		System.out.println("visitMethod:--------------------------------------------------");
		System.out.println("access = " + access);
		System.out.println("name = " + name);
		System.out.println("desc = " + desc);
		System.out.println("signature = " + signature);
		System.out.println("exceptions = " + exceptions);
		System.out.println("end visitMethod==================================================");
		return null;
	}

	/* (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitEnd()
	 */
	@Override
	public void visitEnd() {
		System.out.println("visitEnd!");
	}
	
}
